package com.ruoyi.exam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.entity.TTaskExamPaperAnswer;
import com.ruoyi.exam.service.ITTaskExamPaperAnswerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 答题，记录用户的试卷答题情况Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/exam/answer")
public class TTaskExamPaperAnswerController extends BaseController
{
    @Autowired
    private ITTaskExamPaperAnswerService tTaskExamPaperAnswerService;

    /**
     * 查询答题，记录用户的试卷答题情况列表
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTaskExamPaperAnswer tTaskExamPaperAnswer)
    {
        startPage();
        List<TTaskExamPaperAnswer> list = tTaskExamPaperAnswerService.selectTTaskExamPaperAnswerList(tTaskExamPaperAnswer);
        return getDataTable(list);
    }

    /**
     * 导出答题，记录用户的试卷答题情况列表
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:export')")
    @Log(title = "答题，记录用户的试卷答题情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTaskExamPaperAnswer tTaskExamPaperAnswer)
    {
        List<TTaskExamPaperAnswer> list = tTaskExamPaperAnswerService.selectTTaskExamPaperAnswerList(tTaskExamPaperAnswer);
        ExcelUtil<TTaskExamPaperAnswer> util = new ExcelUtil<TTaskExamPaperAnswer>(TTaskExamPaperAnswer.class);
        util.exportExcel(response, list, "答题，记录用户的试卷答题情况数据");
    }

    /**
     * 获取答题，记录用户的试卷答题情况详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tTaskExamPaperAnswerService.selectTTaskExamPaperAnswerById(id));
    }

    /**
     * 新增答题，记录用户的试卷答题情况
     */
    // @PreAuthorize("@ss.hasPermi('exam:answer:add')")
    @Log(title = "答题，记录用户的试卷答题情况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTaskExamPaperAnswer tTaskExamPaperAnswer)
    {
        return toAjax(tTaskExamPaperAnswerService.insertTTaskExamPaperAnswer(tTaskExamPaperAnswer));
    }

    /**
     * 修改答题，记录用户的试卷答题情况
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:edit')")
    @Log(title = "答题，记录用户的试卷答题情况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTaskExamPaperAnswer tTaskExamPaperAnswer)
    {
        return toAjax(tTaskExamPaperAnswerService.updateTTaskExamPaperAnswer(tTaskExamPaperAnswer));
    }

    /**
     * 删除答题，记录用户的试卷答题情况
     */
    @PreAuthorize("@ss.hasPermi('exam:answer:remove')")
    @Log(title = "答题，记录用户的试卷答题情况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tTaskExamPaperAnswerService.deleteTTaskExamPaperAnswerByIds(ids));
    }

    /**
     * 修复答题记录中的异常题目数据
     * 检测并修复paperAnswer中的空选项题目，自动填充正确答案并重新计分
     */
    @GetMapping("/fix")
    @Anonymous
    public AjaxResult fixAnswerData()
    {
        try {
            int result = tTaskExamPaperAnswerService.fixAllAnswerData();
            return AjaxResult.success("批量修复完成，共修复了 " + result + " 道题目");
        } catch (Exception e) {
            logger.error("批量修复答题记录异常数据失败: {}", e.getMessage(), e);
            return AjaxResult.error("批量修复失败: " + e.getMessage());
        }
    }

    /**
     * 修复指定答题记录中的异常题目数据
     * 检测并修复paperAnswer中的空选项题目，自动填充正确答案并重新计分
     */
    @GetMapping("/fix/{id}")
    @Anonymous
    public AjaxResult fixAnswerData(@PathVariable("id") Long id)
    {
        try {
            int result = tTaskExamPaperAnswerService.fixAnswerData(id);
            return AjaxResult.success("修复完成，共修复了 " + result + " 道题目");
        } catch (Exception e) {
            logger.error("修复答题记录异常数据失败: {}", e.getMessage(), e);
            return AjaxResult.error("修复失败: " + e.getMessage());
        }
    }

}
